asm
  ld sp,$5FFF
  end asm

#include "./library/smsrnd.bas"
#include "./library/smsdelay.bas"
#include "./library/b1r3f0_charmapspaceintruder01.zxi"

dim eee as uinteger at $4010
dim seed as uinteger at $4012
dim ee0 as uinteger at $4014
dim ee1 as uinteger at $4016
dim ee2 as uinteger at $4018
dim ee3 as uinteger at $401A
dim ex0 as uinteger at $401C
dim ey0 as uinteger at $401E

seed=0

sub cosmicalienputchar(txp1 as uinteger,typ1 as uinteger,tch1 as uinteger,tad1 as uinteger):
  dim tlp1 as uinteger at $4000
  for tlp1=0 to 7
    poke $4400+(txp1*256)+(typ1 bxor 31)+(tlp1*32),peek(tad1+(tch1*8)+tlp1)
    next
  end sub


for ee0=0 to 31
  for ee1=0 to 23
    seed=smsrnd(seed):ee3=seed mod (96+9)
    cosmicalienputchar(ee1,ee0,96+(seed mod 9),@charmap01)
    next:next

for ee0=0 to 15
  for ee1=0 to 15
    cosmicalienputchar(ee1+7,ee0+15,ee0*16+ee1,@charmap01)
    next:next


eee=0:ex0=8:ey0=8
do

cosmicalienputchar(2,1,48+int(eee/10000) mod 10,@charmap01)
cosmicalienputchar(3,1,48+int(eee/1000) mod 10,@charmap01)
cosmicalienputchar(4,1,48+int(eee/100) mod 10,@charmap01)
cosmicalienputchar(5,1,48+int(eee/10) mod 10,@charmap01)
cosmicalienputchar(6,1,48+eee mod 10,@charmap01)

ee0=peek($6800) bxor $FF
cosmicalienputchar(4,3,48+int(ee0/100) mod 10,@charmap01)
cosmicalienputchar(5,3,48+int(ee0/10) mod 10,@charmap01)
cosmicalienputchar(6,3,48+ee0 mod 10,@charmap01)

ex0=ex0+((ee0 band 2)/2)-((ee0 band 4)/4)

ee0=peek($6801) bxor $FF
cosmicalienputchar(4,4,48+int(ee0/100) mod 10,@charmap01)
cosmicalienputchar(5,4,48+int(ee0/10) mod 10,@charmap01)
cosmicalienputchar(6,4,48+ee0 mod 10,@charmap01)

ey0=ey0+((ee0 band 2)/2)-((ee0 band 4)/4)

ee0=peek($6802) bxor $FF
cosmicalienputchar(4,5,48+int(ee0/100) mod 10,@charmap01)
cosmicalienputchar(5,5,48+int(ee0/10) mod 10,@charmap01)
cosmicalienputchar(6,5,48+ee0 mod 10,@charmap01)


cosmicalienputchar(ex0,ey0,eee band 127,@charmap01)

eee=eee+1
loop




do:loop

eee=0
for ee0=0 to 31
  for ee1=0 to 23
    cosmicalienputchar(8,8,65,@charmap01)
    seed=smsrnd(seed):ee3=seed mod (96+9)

    cosmicalienputchar(ee1,ee0,eee band $FF,@charmap01)
    'cosmicalienputchar(ee1,ee0,seed band $FF,@charmap01)

    'for ee2=0 to 7
    '  poke $4400+(ee1*256)+(ee0 bxor 31)+(ee2*32),peek(@charmap01+(ee3*8)+ee2)
    '  next
    eee=eee+1
    next:next





do:loop

do

for ee0=0 to 31
  for ee1=0 to 23

    cosmicalienputchar(8,8,65,@charmap01)

    seed=smsrnd(seed):ee3=seed mod (96+9)
    for ee2=0 to 7
      poke $4400+(ee1*256)+(ee0 bxor 31)+(ee2*32),peek(@charmap01+(ee3*8)+ee2)
      next:next:next

loop





do

for ee0=0 to 31
  for ee1=0 to 31
    for ee2=0 to 7
      seed=smsrnd(seed)
      poke $4420+(ee1*256)+(ee0 bxor 31)+(ee2*32),seed
      smsdelay(2000)
      next:next:next

loop







do

for ee2=$4020 to $5FEF
  seed=smsrnd(seed)
  poke ee2,seed
  smsdelay(100)
  next

for ee2=$6000 to $601F
  seed=smsrnd(seed)
  poke ee2,seed
  smsdelay(100)
  next

loop

do:loop

'-------------------------------

'static ADDRESS_MAP_START( cosmica_map, AS_PROGRAM, 8, cosmic_state )
'AM_RANGE(0x0000, 0x3fff) AM_ROM
'AM_RANGE(0x4000, 0x5fff) AM_RAM AM_SHARE("videoram")
'AM_RANGE(0x6000, 0x601f) AM_WRITEONLY AM_SHARE("spriteram")
'AM_RANGE(0x6800, 0x6800) AM_READ_PORT("P1")
'AM_RANGE(0x6801, 0x6801) AM_READ_PORT("P2")
'AM_RANGE(0x6802, 0x6802) AM_READ_PORT("DSW")
'AM_RANGE(0x6803, 0x6803) AM_READ(cosmica_pixel_clock_r)
'AM_RANGE(0x7000, 0x700b) AM_WRITE(cosmica_sound_output_w)
'AM_RANGE(0x700c, 0x700d) AM_WRITE(cosmic_color_register_w)
'AM_RANGE(0x700f, 0x700f) AM_WRITE(flip_screen_w)
'ADDRESS_MAP_END

